package br.edu.utfpr.backuptools.BackupTools.view.telas.principal.tabela;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.SoftBevelBorder;

import br.edu.utfpr.backuptools.BackupTools.controller.validator.RetornoValidacao;
import br.edu.utfpr.backuptools.BackupTools.model.pojo.Backup;
import br.edu.utfpr.backuptools.BackupTools.util.Mensagem;
import br.edu.utfpr.backuptools.BackupTools.util.backup.ZipUtil;
import br.edu.utfpr.backuptools.BackupTools.util.worker.ProgressDialogTask;
import br.edu.utfpr.backuptools.BackupTools.util.worker.WorkerBackup;
import br.edu.utfpr.backuptools.BackupTools.util.worker.WorkerRestore;
import br.edu.utfpr.backuptools.BackupTools.view.action.AbstractAction;
import br.edu.utfpr.backuptools.BackupTools.view.action.backup.ExcluirBackupAction;
import br.edu.utfpr.backuptools.BackupTools.view.action.backup.GerarBackupIndividual;
import br.edu.utfpr.backuptools.BackupTools.view.telas.principal.JanelaPrincipal;

public class BackupCellComponent extends JPanel{
	Backup backup;
	JButton gerarButton,excluirButton,restoreButton;
	JLabel texto,descricao;
	JPanel botoes;
	StringBuilder str;
	
	AbstractAction<Backup> action = new GerarBackupIndividual();
	
	public BackupCellComponent(final JanelaPrincipal janelaPrincipal){
		
		gerarButton = new JButton();
		gerarButton.setToolTipText("Gerar backup");
		gerarButton.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/Play-Normal-icon.png")));
		gerarButton.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent arg0) {
		    	String title = "Aguarde";
		  		String message = "Gerando backup "+ backup.getDescricao();
		  		ProgressDialogTask pdt = new ProgressDialogTask(new JFrame(), title, message);
		  		WorkerBackup work = new WorkerBackup(backup);
		  		pdt.setModal(true);
		  		pdt.addTask(work);
		  		pdt.run();
		  		
		  		janelaPrincipal.populaTabela();
	      }
	    });
		
		excluirButton = new JButton();
		excluirButton.setToolTipText("Excluir");
		excluirButton.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/Trash-icon.png")));
		excluirButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int op = JOptionPane.showConfirmDialog(null,"Deseja excluir o backup "+backup.getDescricao(),"Excluir",JOptionPane.YES_NO_OPTION);
				if(op == 0){
					AbstractAction<Backup> action = new ExcluirBackupAction();
					RetornoValidacao rv = action.execute(backup);
					JanelaPrincipal.addLog(rv.isOk() ? "Backup " + backup.getDescricao() + " excluido com sucesso" : rv.getErro());
					janelaPrincipal.populaTabela();
					janelaPrincipal.updateSelecionado(null);
				}
			}
		});
		
		restoreButton = new JButton();
		restoreButton.setToolTipText("Fazer restore do backup");
		restoreButton.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/backup-restore-icon.png")));
		restoreButton.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(ZipUtil.diretorioExiste(backup)){
					int op = JOptionPane.showConfirmDialog(null,"A pasta de origem ainda existe, deseja substituir pelo restore?","Atenção",JOptionPane.YES_NO_OPTION);
					
					if(op != 0){
						return;
					}
					
					ZipUtil.deleteDiretorio(backup);
				}
				
				String title = "Aguarde";
		  		String message = "Gerando restore "+ backup.getDescricao();
		  		ProgressDialogTask pdt = new ProgressDialogTask(new JFrame(), title, message);
		  		WorkerRestore work = new WorkerRestore(backup);
		  		pdt.setModal(true);
		  		pdt.addTask(work);
		  		pdt.run();
		  		
		  		RetornoValidacao rv = work.getRv();
		  		
		    	janelaPrincipal.showMessage(rv.isOk() ? "Restore executado com sucesso" : rv.getErro(), JOptionPane.INFORMATION_MESSAGE); 
			}
		});
		
		this.setLayout(new BorderLayout());
	    setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));  
	    Font f = new Font("Arial",Font.BOLD , 12);
	    
	    descricao = new JLabel();
	    descricao.setFont(f);
	    texto = new JLabel();
	    
	    botoes = new JPanel();
	    botoes.setLayout(new BorderLayout());
	    botoes.add(gerarButton,BorderLayout.WEST);
	    botoes.add(restoreButton,BorderLayout.CENTER);
	    botoes.add(excluirButton,BorderLayout.EAST);
	   
	    add(descricao,BorderLayout.WEST);
	    add(texto,BorderLayout.CENTER);
	    add(botoes,BorderLayout.EAST);
	}
	
	public void updateData(Backup backup, boolean isSelected, JTable table) {
		this.backup = backup;
		descricao.setText("Backup:    ");  
		
		str = new StringBuilder();
		str.append(backup.getDescricao());
		str.append("  -  ");
		str.append("Último backup: ");
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");  
		str.append(backup.getDataUltimoBackup() == null ? "nenhuma execução" : format.format(backup.getDataUltimoBackup()));
		
		texto.setText(str.toString());
		
	    if (isSelected) {
	    	setBackground(new Color(214, 217, 223));
	    	botoes.setBackground(new Color(214, 217, 223));
	    }else{
	    	setBackground(new Color(255, 255, 255));
	    	botoes.setBackground(new Color(255, 255, 255));
	    }
	}
	
	
	
}
